import md5 from 'md5'

var storeUtil = {
    isEmpty(i){
        if(i === ' ' || i === "undefined" || i === null){
            return true
        }
    }
}
export default function requestSign (params) {
    var keys = Object.keys(params).sort()
    var newParams = {}
    var str = ''
    for (var i = 0; i < keys.length; i++) {
        var value = params[keys[i]]
        if (storeUtil.isEmpty(value) || value.length <= 0) {
            continue
        } else {
            newParams[keys[i]] = params[keys[i]]
        }

        var key = keys[i]
        // 仅能处理数组成员为基本类型或者统一对象类型的数组
        if (Object.prototype.toString.call(value) === '[object Array]') {
            key += '[]'
            // 格式化值
            value = JSON.stringify(value).replace(/},{/g, '}, {')
            if (value.indexOf('{') < 0) {
                value = '[' + params[keys[i]].join(', ') + ']'
            }
        }
        if (str.length === 0) {
            str += key + '=' + value
        } else {
            str += '&' + key + '=' + value
        }
    }
    var sign = md5(str).toUpperCase()
    console.log('str: ' + str)
    console.log('sign: ' + sign)
    newParams.sign = sign
    return newParams
}